/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package paymentclient;

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import org.netbeans.microedition.lcdui.LoginScreen;
import org.netbeans.microedition.lcdui.SplashScreen;
import javax.microedition.rms.RecordStore;

/**
 * @author Administrator
 */
public class Micropayment extends MIDlet implements CommandListener {

    private boolean midletPaused = false;

    //<editor-fold defaultstate="collapsed" desc=" Generated Fields ">//GEN-BEGIN:|fields|0|
    private java.util.Hashtable __previousDisplayables = new java.util.Hashtable();
    private SplashScreen frmStartup;
    private LoginScreen frmLogin;
    private Form mainScreen;
    private Form frmOption;
    private TextField txtExpired;
    private TextField txtPaymentService;
    private Command EXIT_COMMAND;
    private Command PROCESS_COMMAND;
    private Command OPTION_COMMAND;
    private Command BACK_COMMAND;
    private Command SAVE_COMMAND;
    //</editor-fold>//GEN-END:|fields|0|

    /**
     * The Micropayment constructor.
     */
    public Micropayment() {
    }

    //<editor-fold defaultstate="collapsed" desc=" Generated Methods ">//GEN-BEGIN:|methods|0|
    /**
     * Switches a display to previous displayable of the current displayable.
     * The <code>display</code> instance is obtain from the <code>getDisplay</code> method.
     */
    private void switchToPreviousDisplayable() {
        Displayable __currentDisplayable = getDisplay().getCurrent();
        if (__currentDisplayable != null) {
            Displayable __nextDisplayable = (Displayable) __previousDisplayables.get(__currentDisplayable);
            if (__nextDisplayable != null) {
                switchDisplayable(null, __nextDisplayable);
            }
        }
    }
    //</editor-fold>//GEN-END:|methods|0|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: initialize ">//GEN-BEGIN:|0-initialize|0|0-preInitialize
    /**
     * Initilizes the application.
     * It is called only once when the MIDlet is started. The method is called before the <code>startMIDlet</code> method.
     */
    private void initialize() {//GEN-END:|0-initialize|0|0-preInitialize
        // write pre-initialize user code here
        frmStartup = new SplashScreen(getDisplay());//GEN-BEGIN:|0-initialize|1|0-postInitialize
        frmStartup.setTitle("");
        frmStartup.setCommandListener(this);
        frmStartup.setText("Welcome to Micro Payment\nMidlet");
        frmStartup.setTimeout(2000);
        frmLogin = new LoginScreen(getDisplay());
        frmLogin.setLabelTexts("Username", "Password");
        frmLogin.setTitle("Login");
        frmLogin.addCommand(LoginScreen.LOGIN_COMMAND);
        frmLogin.addCommand(getEXIT_COMMAND());
        frmLogin.addCommand(getOPTION_COMMAND());
        frmLogin.setCommandListener(this);
        frmLogin.setBGColor(-3355444);
        frmLogin.setFGColor(0);
        frmLogin.setUseLoginButton(false);
        frmLogin.setLoginButtonText("Login");//GEN-END:|0-initialize|1|0-postInitialize
        // write post-initialize user code here
    }//GEN-BEGIN:|0-initialize|2|
    //</editor-fold>//GEN-END:|0-initialize|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: startMIDlet ">//GEN-BEGIN:|3-startMIDlet|0|3-preAction
    /**
     * Performs an action assigned to the Mobile Device - MIDlet Started point.
     */
    public void startMIDlet() {//GEN-END:|3-startMIDlet|0|3-preAction
        // write pre-action user code here
        switchDisplayable(null, frmStartup);//GEN-LINE:|3-startMIDlet|1|3-postAction
        // write post-action user code here
    }//GEN-BEGIN:|3-startMIDlet|2|
    //</editor-fold>//GEN-END:|3-startMIDlet|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: resumeMIDlet ">//GEN-BEGIN:|4-resumeMIDlet|0|4-preAction
    /**
     * Performs an action assigned to the Mobile Device - MIDlet Resumed point.
     */
    public void resumeMIDlet() {//GEN-END:|4-resumeMIDlet|0|4-preAction
        // write pre-action user code here
        switchDisplayable(null, frmLogin);//GEN-LINE:|4-resumeMIDlet|1|4-postAction
        // write post-action user code here
    }//GEN-BEGIN:|4-resumeMIDlet|2|
    //</editor-fold>//GEN-END:|4-resumeMIDlet|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: switchDisplayable ">//GEN-BEGIN:|5-switchDisplayable|0|5-preSwitch
    /**
     * Switches a current displayable in a display. The <code>display</code> instance is taken from <code>getDisplay</code> method. This method is used by all actions in the design for switching displayable.
     * @param alert the Alert which is temporarily set to the display; if <code>null</code>, then <code>nextDisplayable</code> is set immediately
     * @param nextDisplayable the Displayable to be set
     */
    public void switchDisplayable(Alert alert, Displayable nextDisplayable) {//GEN-END:|5-switchDisplayable|0|5-preSwitch
        // write pre-switch user code here
        Display display = getDisplay();//GEN-BEGIN:|5-switchDisplayable|1|5-postSwitch
        Displayable __currentDisplayable = display.getCurrent();
        if (__currentDisplayable != null  &&  nextDisplayable != null) {
            __previousDisplayables.put(nextDisplayable, __currentDisplayable);
        }
        if (alert == null) {
            display.setCurrent(nextDisplayable);
        } else {
            display.setCurrent(alert, nextDisplayable);
        }//GEN-END:|5-switchDisplayable|1|5-postSwitch
        // write post-switch user code here
    }//GEN-BEGIN:|5-switchDisplayable|2|
    //</editor-fold>//GEN-END:|5-switchDisplayable|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: commandAction for Displayables ">//GEN-BEGIN:|7-commandAction|0|7-preCommandAction
    /**
     * Called by a system to indicated that a command has been invoked on a particular displayable.
     * @param command the Command that was invoked
     * @param displayable the Displayable where the command was invoked
     */
    public void commandAction(Command command, Displayable displayable) {//GEN-END:|7-commandAction|0|7-preCommandAction
        // write pre-action user code here
        if (displayable == frmLogin) {//GEN-BEGIN:|7-commandAction|1|25-preAction
            if (command == EXIT_COMMAND) {//GEN-END:|7-commandAction|1|25-preAction
                // write pre-action user code here
                exitMIDlet();//GEN-LINE:|7-commandAction|2|25-postAction
                // write post-action user code here
            } else if (command == LoginScreen.LOGIN_COMMAND) {//GEN-LINE:|7-commandAction|3|19-preAction
                // write pre-action user code here
                checkLogin();
//GEN-LINE:|7-commandAction|4|19-postAction
                // write post-action user code here
            } else if (command == OPTION_COMMAND) {//GEN-LINE:|7-commandAction|5|57-preAction
                // write pre-action user code here
                switchDisplayable(null, getFrmOption());//GEN-LINE:|7-commandAction|6|57-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|7|39-preAction
        } else if (displayable == frmOption) {
            if (command == BACK_COMMAND) {//GEN-END:|7-commandAction|7|39-preAction
                // write pre-action user code here
                switchToPreviousDisplayable();//GEN-LINE:|7-commandAction|8|39-postAction
                // write post-action user code here
            } else if (command == SAVE_COMMAND) {//GEN-LINE:|7-commandAction|9|56-preAction
                // write pre-action user code here
//GEN-LINE:|7-commandAction|10|56-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|11|15-preAction
        } else if (displayable == frmStartup) {
            if (command == SplashScreen.DISMISS_COMMAND) {//GEN-END:|7-commandAction|11|15-preAction
                // write pre-action user code here
                switchDisplayable(null, frmLogin);//GEN-LINE:|7-commandAction|12|15-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|13|48-preAction
        } else if (displayable == mainScreen) {
            if (command == EXIT_COMMAND) {//GEN-END:|7-commandAction|13|48-preAction
                // write pre-action user code here
//GEN-LINE:|7-commandAction|14|48-postAction
                // write post-action user code here
            } else if (command == PROCESS_COMMAND) {//GEN-LINE:|7-commandAction|15|31-preAction
                // write pre-action user code here
//GEN-LINE:|7-commandAction|16|31-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|17|7-postCommandAction
        }//GEN-END:|7-commandAction|17|7-postCommandAction
        // write post-action user code here
    }//GEN-BEGIN:|7-commandAction|18|
    //</editor-fold>//GEN-END:|7-commandAction|18|





    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: mainScreen ">//GEN-BEGIN:|21-getter|0|21-preInit
    /**
     * Returns an initiliazed instance of mainScreen component.
     * @return the initialized component instance
     */
    public Form getMainScreen() {
        if (mainScreen == null) {//GEN-END:|21-getter|0|21-preInit
            // write pre-init user code here
            mainScreen = new Form("Micro Payment on Mobile");//GEN-BEGIN:|21-getter|1|21-postInit
            mainScreen.addCommand(getPROCESS_COMMAND());
            mainScreen.addCommand(getEXIT_COMMAND());
            mainScreen.setCommandListener(this);//GEN-END:|21-getter|1|21-postInit
            // write post-init user code here
        }//GEN-BEGIN:|21-getter|2|
        return mainScreen;
    }
    //</editor-fold>//GEN-END:|21-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: EXIT_COMMAND ">//GEN-BEGIN:|24-getter|0|24-preInit
    /**
     * Returns an initiliazed instance of EXIT_COMMAND component.
     * @return the initialized component instance
     */
    public Command getEXIT_COMMAND() {
        if (EXIT_COMMAND == null) {//GEN-END:|24-getter|0|24-preInit
            // write pre-init user code here
            EXIT_COMMAND = new Command("Exit", Command.EXIT, 0);//GEN-LINE:|24-getter|1|24-postInit
            // write post-init user code here
        }//GEN-BEGIN:|24-getter|2|
        return EXIT_COMMAND;
    }
    //</editor-fold>//GEN-END:|24-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: frmOption ">//GEN-BEGIN:|35-getter|0|35-preInit
    /**
     * Returns an initiliazed instance of frmOption component.
     * @return the initialized component instance
     */
    public Form getFrmOption() {
        if (frmOption == null) {//GEN-END:|35-getter|0|35-preInit
            // write pre-init user code here
            frmOption = new Form("Option", new Item[] { getTxtPaymentService(), getTxtExpired() });//GEN-BEGIN:|35-getter|1|35-postInit
            frmOption.addCommand(getBACK_COMMAND());
            frmOption.addCommand(getSAVE_COMMAND());
            frmOption.setCommandListener(this);//GEN-END:|35-getter|1|35-postInit
            // write post-init user code here
        }//GEN-BEGIN:|35-getter|2|
        return frmOption;
    }
    //</editor-fold>//GEN-END:|35-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: PROCESS_COMMAND ">//GEN-BEGIN:|30-getter|0|30-preInit
    /**
     * Returns an initiliazed instance of PROCESS_COMMAND component.
     * @return the initialized component instance
     */
    public Command getPROCESS_COMMAND() {
        if (PROCESS_COMMAND == null) {//GEN-END:|30-getter|0|30-preInit
            // write pre-init user code here
            PROCESS_COMMAND = new Command("Process", Command.ITEM, 0);//GEN-LINE:|30-getter|1|30-postInit
            // write post-init user code here
        }//GEN-BEGIN:|30-getter|2|
        return PROCESS_COMMAND;
    }
    //</editor-fold>//GEN-END:|30-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: OPTION_COMMAND ">//GEN-BEGIN:|32-getter|0|32-preInit
    /**
     * Returns an initiliazed instance of OPTION_COMMAND component.
     * @return the initialized component instance
     */
    public Command getOPTION_COMMAND() {
        if (OPTION_COMMAND == null) {//GEN-END:|32-getter|0|32-preInit
            // write pre-init user code here
            OPTION_COMMAND = new Command("Option", Command.ITEM, 0);//GEN-LINE:|32-getter|1|32-postInit
            // write post-init user code here
        }//GEN-BEGIN:|32-getter|2|
        return OPTION_COMMAND;
    }
    //</editor-fold>//GEN-END:|32-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: BACK_COMMAND ">//GEN-BEGIN:|38-getter|0|38-preInit
    /**
     * Returns an initiliazed instance of BACK_COMMAND component.
     * @return the initialized component instance
     */
    public Command getBACK_COMMAND() {
        if (BACK_COMMAND == null) {//GEN-END:|38-getter|0|38-preInit
            // write pre-init user code here
            BACK_COMMAND = new Command("Back", Command.BACK, 0);//GEN-LINE:|38-getter|1|38-postInit
            // write post-init user code here
        }//GEN-BEGIN:|38-getter|2|
        return BACK_COMMAND;
    }
    //</editor-fold>//GEN-END:|38-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: txtPaymentService ">//GEN-BEGIN:|49-getter|0|49-preInit
    /**
     * Returns an initiliazed instance of txtPaymentService component.
     * @return the initialized component instance
     */
    public TextField getTxtPaymentService() {
        if (txtPaymentService == null) {//GEN-END:|49-getter|0|49-preInit
            // write pre-init user code here
            txtPaymentService = new TextField("Payment service:", null, 32, TextField.ANY);//GEN-LINE:|49-getter|1|49-postInit
            // write post-init user code here
        }//GEN-BEGIN:|49-getter|2|
        return txtPaymentService;
    }
    //</editor-fold>//GEN-END:|49-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: txtExpired ">//GEN-BEGIN:|50-getter|0|50-preInit
    /**
     * Returns an initiliazed instance of txtExpired component.
     * @return the initialized component instance
     */
    public TextField getTxtExpired() {
        if (txtExpired == null) {//GEN-END:|50-getter|0|50-preInit
            // write pre-init user code here
            txtExpired = new TextField("Expired time:", null, 32, TextField.ANY);//GEN-LINE:|50-getter|1|50-postInit
            // write post-init user code here
        }//GEN-BEGIN:|50-getter|2|
        return txtExpired;
    }
    //</editor-fold>//GEN-END:|50-getter|2|









    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: SAVE_COMMAND ">//GEN-BEGIN:|55-getter|0|55-preInit
    /**
     * Returns an initiliazed instance of SAVE_COMMAND component.
     * @return the initialized component instance
     */
    public Command getSAVE_COMMAND() {
        if (SAVE_COMMAND == null) {//GEN-END:|55-getter|0|55-preInit
            // write pre-init user code here
            SAVE_COMMAND = new Command("Save", Command.ITEM, 0);//GEN-LINE:|55-getter|1|55-postInit
            // write post-init user code here
        }//GEN-BEGIN:|55-getter|2|
        return SAVE_COMMAND;
    }
    //</editor-fold>//GEN-END:|55-getter|2|



    /**
     * Returns a display instance.
     * @return the display instance.
     */
    public Display getDisplay () {
        return Display.getDisplay(this);
    }

    /**
     * Exits MIDlet.
     */
    public void exitMIDlet() {
        switchDisplayable (null, null);
        destroyApp(true);
        notifyDestroyed();
    }

    /**
     * Called when MIDlet is started.
     * Checks whether the MIDlet have been already started and initialize/starts or resumes the MIDlet.
     */
    public void startApp() {
        if (midletPaused) {
            resumeMIDlet ();
        } else {
            initialize ();
            startMIDlet ();
        }
        midletPaused = false;
    }

    /**
     * Called when MIDlet is paused.
     */
    public void pauseApp() {
        midletPaused = true;
    }

    /**
     * Called to signal the MIDlet to terminate.
     * @param unconditional if true, then the MIDlet has to be unconditionally terminated and all resources has to be released.
     */
    public void destroyApp(boolean unconditional) {
    }
    
    private void checkLogin(){
        if ((frmLogin.getUsername().equals("huynm") )&&(frmLogin.getPassword().equals("123456"))) {
            switchDisplayable(null, getMainScreen());
            return;
        }
        frmLogin.setTitle("Wrong username or password");
        switchDisplayable(null, frmLogin);
    }
}
